<?php

namespace App\Logging;

use Monolog\Formatter\JsonFormatter as BaseJsonFormatter;

class JsonFormatter extends BaseJsonFormatter
{
    public function format(array $record)
    {
        $arr = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
        $length = count($arr);
        $deep = 7;
        if ($length < 8) {
            // swoole 线程
            $record['extra'] = $arr[$length - 1];
            return parent::format($record);
        } elseif (!isset($arr[7]['file']) || strpos($arr[7]['file'], '/src/vendor/') > 0 || count($arr) < 9) {
            // console 模式下，少一层
            $deep = 6;
        }
        $arr = array_slice($arr, $deep);
        $record['extra'] = [
            'file' => $arr[0]['file'],
            'function' => $arr[1]['function'],
            'line' => $arr[0]['line'],
        ];
        return parent::format($record);
    }
}
